package com.yixun.qfbao.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

public class PasswordUtil {
    public static final String DEFAULT_PWD = "x1234567";
   private static String KEY="qfb_jr_";
   public static String md5Hex(String password){
	   String tempPwd=KEY+password;
	   return DigestUtils.md5Hex(getContentBytes(tempPwd, "utf-8"));
   }

        public static String defaultPwd(){
        return md5Hex(DEFAULT_PWD);
    }

    //生成随机数字和字母,
    public static String generateRandomPwd(int length) {

        String val = "";
        Random random = new Random();
        //length为几位密码
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static String generateRandomNumPwd(int length) {

        char[] chars = new char[8];
        Random random = new Random();
        //length为几位密码
        for(int i = 0; i < length; i++) {
            //输出是数字
            chars[i] = (char) ('0' + random.nextInt(10));
        }
        return new String(chars);
    }

   
   private static byte[] getContentBytes(String content, String charset) {
       if (charset == null || "".equals(charset)) {
           return content.getBytes();
       }
       try {
           return content.getBytes(charset);
       } catch (UnsupportedEncodingException e) {
           throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
       }
   }

    public static void main(String[] args) {
        System.out.printf(generateRandomNumPwd(8));
    }

}
